/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.cheln.explorer.cpnt.adapter;

import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.view.View;
import cn.cheln.explorer.beans.FileInfo;
import cn.cheln.support.highlight.KeyMatcher;
import cn.cheln.support.highlight.TextHighLightDecorator;
import cn.power.shine.R;

/**
 * 
 * @author chenliang
 */
public class SearchAdapter extends CountAdapter {

	KeyMatcher matcher = new KeyMatcher("");
	TextHighLightDecorator decoratorHighLight;
	List<String> fieldstoimpose;
	int color;

	public SearchAdapter(Context context, AdaptInfo listViewHolder) {
		super(context, listViewHolder);
	}

	public void setHighlightInfo(String matcherText, String[] fieldstoimpose,
			int color) {
		try {
			decoratorHighLight = new TextHighLightDecorator(color);
			switchKeyWords(matcherText);
			this.fieldstoimpose = Arrays.<String> asList(fieldstoimpose);
			this.color = color;
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void switchKeyWords(String matcherText) {
		this.matcher = new KeyMatcher(matcherText);
		decoratorHighLight.setMatcher(matcher);

	}

	@Override
	protected void getViewInDetail(Object item, int position, View convertView) {
		// TODO Auto-generated method stub

		try {

			if (item != null) {
				if (((FileInfo) item).count == -2) {
					((FileInfo) item).sizeText = "";
				}
			} else {
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		super.getViewInDetail(item, position, convertView);
		convertView.findViewById(R.id.subFileNum).setVisibility(View.GONE);
	}

	@Override
	protected void findAndBindView(View convertView, int pos, Object item,
			String name, Object value) {
		if (decoratorHighLight != null && fieldstoimpose != null) {
			if (fieldstoimpose.contains(name)) {
				value = decoratorHighLight.getDecorated(value.toString(),
                        ((FileInfo) item).getName(),((FileInfo) item).sortKey);
			}
		}
		super.findAndBindView(convertView, pos, item, name, value);
	}

	@Override
	public FileInfo getSimpleFileItem(int pos) {
		return ((FileInfo) this.getItem(pos));
	}
}